package presentation;

import nd.main.*;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeItemAdapter extends ArrayAdapter<RowData> {
	LayoutInflater mInflater;
	Context mContext;
	
	public HomeItemAdapter( Context context, 
							int resource,
							int textViewResourceId, 
							List<RowData> objects) 
	{
		super(context, resource, textViewResourceId, objects);
		
		mContext = context;
	}
	
	// ArrayAdapter will call this method for each time it load an item to the list when the ListView inits
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
	   HomeItemViewHolder holder = null;
	   ImageView imageView = null;
	   TextView title = null;
	   TextView detail = null;
	   
	   
	   RowData rowData= getItem(position);
	   
		if(null == convertView)
		{ 
			// Create a view from a specified layout with no data
			mInflater = (LayoutInflater)mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_home, null);
			holder = new HomeItemViewHolder(convertView);
			//
			convertView.setTag(holder);
		}
		
		
		holder = (HomeItemViewHolder) convertView.getTag();
		
		// Set item image
		imageView = holder.getImageView();
		imageView.setImageResource(R.drawable.test);
		
		// Set item title
		title = holder.getTitleView();
		title.setText(rowData.mTitle);
		
		// Set item detail info
		detail = holder.getDateView();
		detail.setText(rowData.formatDateTime());                                                     

		return convertView;
	}
}
